package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.dao.VipCustomerLogDao;
import cc.linker.steplink.crm.merchanth5.service.ColumnService;
import cc.linker.steplink.crm.merchanth5.service.ZsContentService;
import cc.linker.steplink.crm.merchanth5.service.ZsGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 专栏
 */
@RequestMapping("/h5/column")
@RestController
public class ColumnController {

    @Autowired
    private ColumnService columnService;
    @Autowired
    private RequestFilter requestFilter;
    @Autowired
    private ZsContentService zsContentService;
    @Autowired
    private ZsGoodsService zsGoodsService;
    @Autowired
    private VipCustomerLogDao vipCustomerLogDao;

    /**
     * 专栏商品详情
     *
     * @param goodsId
     * @return
     * @throws GlobalException
     */
    @GetMapping("/info")
    public Map<String, Object> info(String goodsId,
                                    @RequestParam(defaultValue = "") String shareCustomerId,
                                    @RequestParam(defaultValue = "1") Integer shareType,
                                    HttpServletRequest request) throws GlobalException {
        if (StringUtils.isEmpty(goodsId)) {
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        zsGoodsService.setGoodsShareLog(Long.parseLong(goodsId),shareCustomerId,shareType,customerId);
        return columnService.getDetail(goodsId, customerId);

    }

    /**
     * 积分专栏商品详情
     *
     * @param goodsId
     * @return
     * @throws GlobalException
     */
    @GetMapping("/integralInfo")
    public Map<String, Object> integralInfo(String goodsId,
                                            @RequestParam(defaultValue = "") String shareCustomerId,
                                            @RequestParam(defaultValue = "1") Integer shareType,
                                            HttpServletRequest request) throws GlobalException {
        if (StringUtils.isEmpty(goodsId)) {
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();

        Map<String, Object> integralGoodsInfo = zsContentService.getIntegralGoodsInfo(goodsId);
        if(null == integralGoodsInfo){
            throw GlobalException.build("参数错误");
        }
        zsGoodsService.setGoodsShareLog(Long.parseLong(integralGoodsInfo.get("originalGoodsId").toString()),shareCustomerId,shareType,customerId);
        Map<String, Object> goodsInfo = columnService.getDetail(integralGoodsInfo.get("originalGoodsId").toString(), customerId);
        goodsInfo.put("shareTitle",integralGoodsInfo.get("shareInfo"));
        goodsInfo.put("shareInfo",integralGoodsInfo.get("shareInfo"));
        goodsInfo.put("belongId",integralGoodsInfo.get("belongId"));
        goodsInfo.put("moneyAmount",integralGoodsInfo.get("moneyAmount"));
        goodsInfo.put("integral",integralGoodsInfo.get("integral"));
        goodsInfo.put("quotaNum",integralGoodsInfo.get("quotaNum"));
        goodsInfo.put("quotaLevel",integralGoodsInfo.get("quotaLevel"));
        goodsInfo.put("goodsCount",integralGoodsInfo.get("goodsCount"));
        goodsInfo.put("originalCost",integralGoodsInfo.get("originalCost"));
        if(!StringUtils.isEmpty(integralGoodsInfo.get("quotaVips"))){
            List<Map> vipList = zsContentService.getQuotaVipList(integralGoodsInfo.get("quotaVips").toString(),customerId);
            goodsInfo.put("quotaVipList",vipList);
        }
        return goodsInfo;

    }



    /**
     * 分页获取专栏下的内容列表
     *
     * @param columnId  专栏id
     * @param pageSize  页数
     * @param pageIndex 页容量
     * @param order     排序规则 按照创建时间正序（other value）/倒序（1，默认）
     * @return
     * @throws GlobalException
     */
    @GetMapping("/contents")
    public PageInfo<Map<String, Object>> contents(String columnId,
                                                  Integer order,
                                                  @RequestParam(defaultValue = "20") Integer pageSize,
                                                  @RequestParam(defaultValue = "1") Integer pageIndex,
                                                  @RequestParam(defaultValue = "-1") Integer integralGoodsId,
                                                  HttpServletRequest request) throws GlobalException {
        if (StringUtils.isEmpty(columnId)) {
            throw GlobalException.build("参数错误");
        }
        String orderBy = "t2.SERIAL_NUM ";
        if (order == null) {
            String sort = columnService.getSortByColumnId(columnId);
            orderBy += sort;
        } else {
            orderBy += (order == 1 ? "desc" : "asc");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(columnService.getColumnContents(integralGoodsId,columnId, customerId, orderBy));
    }

    /**
     * 确认订单
     *
     * @param columnId 专栏id
     * @return
     * @throws GlobalException
     */
    @GetMapping("/purchase")
    public Map purchase(String columnId, HttpServletRequest request) throws GlobalException {
        if (StringUtils.isEmpty(columnId)) {
            throw GlobalException.build("参数错误");
        }
        return columnService.getOrder(columnId);
    }
}
